package com.ld.security.common.core.util;

import com.ld.security.common.core.anno.EventKey;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;

@Slf4j
public class EventUtil {

    /**
     * 同步分发
     * @param target
     * @param methodName
     * @param args
     * @throws Throwable
     */
    @SneakyThrows
  public static void asyncDispatch(Class target, String methodName, Object... args) {

      Object bean =  SpringUtil.getBean(target);
      if(bean == null){
          log.warn("对象不存在："+target);
      }
//      Class[] clazzs=new Class<?>[args.length];
//      for(int i = 0;i<args.length;i++){
//          clazzs[i]=args[i].getClass();
//      }

      Object val =   ReflectionUtil.invokeMethod(bean,methodName,args);

      log.warn("事件执行完毕");
//
//      Method eventMethod =  ReflectionUtils.findMethod(target,methodName,clazzs);
//      eventMethod.invoke(bean,args);

  }

    /**
     * 异步分发
     * @param target
     * @param methodName
     * @param args
     * @throws Throwable
     */
        public static void dispatch(Class target,String methodName,Object ... args){



        CompletableFuture.runAsync(() -> {

            try{
                asyncDispatch(target,methodName,args);

            }catch (Exception e){
                e.printStackTrace();
            }

        });


    }


}
